﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Member_address_edit : OTS.MemberPage
{
    private OTS.Model.custom custom = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!TestLogin(HttpContext.Current))
            return;
        
        custom = GetCustom(HttpContext.Current);
        if (!Page.IsPostBack)
        {
            ViewState["addID"] = 0;
            if (Request.QueryString["addId"] != null)
            {
                string strID = Request.QueryString["addId"].ToString() ;
                OTS.BLL.DeliverAddressBLL bll = new OTS.BLL.DeliverAddressBLL();
                OTS.Model.deliverAddress model = bll.GetModel(int.Parse(strID));
                if (model != null)
                {
                    if (model.cusID == custom.cusID) //检验是否为客户自己的地址
                    {
                        ViewState["addID"] = model.addressID;
                        this.txtName.Text = model.consignee;
                        this.txtAddress.Text = model.address;
                        this.txtPhone.Text = model.phone;
                        this.txtPhone1.Text = model.phone1;
                        this.txtPostCode.Text = model.postCode;
                        if (model.isDefault == 1)
                        {
                            this.cbDefault.Checked = true;
                        }
                        this.txtProvince.Text = model.province;
                        this.txtCity.Text = model.city;
                        this.txtArea.Text = model.area;
                    }
                }

            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        int addID = 0;
        if (ViewState["addID"] != null)
        {
            if (OTS.Common.Help.IsNumber( ViewState["addID"].ToString()))
            {
                addID = int.Parse(ViewState["addID"].ToString());
            }
        }
        OTS.Model.deliverAddress model = new OTS.Model.deliverAddress();
        model.address = this.txtAddress.Text;
        
        model.area = this.txtArea.Text;
        model.city = this.txtCity.Text;
        model.consignee = this.txtName.Text;
        model.cusID = custom.cusID;
        model.isDefault = this.cbDefault.Checked ? 1 : 0;
        model.phone = this.txtPhone.Text;
        model.phone1 = this.txtPhone1.Text;
        model.postCode = this.txtPostCode.Text;
        model.province = this.txtProvince.Text;
        OTS.BLL.DeliverAddressBLL bll = new OTS.BLL.DeliverAddressBLL();
        bool bRet;
        if (addID > 0) //update
        {
            model.addressID = addID;
            bRet = bll.Update(model);
        }
        else //ADD
        {
            bRet = bll.Add(model);
        }
        if (bRet)
        {
            OTS.Common.Jscript.AlertAndRedirect("保存成功！", "Member-address.aspx");
        }

    }
}
